package main

import (
	"github.com/gin-gonic/gin"
	"github.com/go-xorm/xorm"
	"github.com/micro/go-micro/web"
	"log"
	"mall/admin/web/controller"
	serviceImp "mall/admin/web/service"
	"time"
)


func main()  {


	// Create service
	service := web.NewService(
		web.Name("go.micro.api.admin"),
		web.Version("laster"),
		web.RegisterTTL(time.Second*10),
		web.Metadata(map[string]string{"name":"imser"}),
		web.RegisterInterval(time.Second*5),
	)

	service.Init()

	router := gin.Default()
	//xorm引擎
	engine, err := xorm.NewEngine("mysql", "root:root@/demo?charset=utf8")
	if err!=nil{
		log.Fatalf("數據庫連接失敗:%v",err)
	}
	engine.ShowSQL(true)
	userService:=serviceImp.NewUserService(engine)
	userCtrl:=controller.NewUserController(userService)
	group:=router.Group("admin")
	//group.Use(middleware.TokenValidMiddleware())
	{
		group.GET("/login", func(ctx *gin.Context) {

			ctx.String(200,"login success")

		})
		//group.POST("/login",userCtrl.Login)
		group.POST("/register",userCtrl.Register)
		group.POST("/food",userCtrl.SetFood)
		group.POST("/list",userCtrl.FoodList)
		group.GET("/join",userCtrl.Join)
	}

	// Register Handler
	service.Handle("/", router)
	// Run server
	if err := service.Run(); err != nil {
		log.Fatal(err)
	}

}
